﻿<META http-equiv="refresh" content="<?php echo rand(500,600); ?>" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px;}
td{font-size:12px;}
</style>
<?php


/**
 * 国际足球视频数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

date_default_timezone_set("Asia/Shanghai");
@ $date = $_GET["date"];

if ($date == "") {
	$searchDate = date("Ymd");
	$date = date("Y-m-d");
}

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

//先取得当前数据库中的数据
$sql = "select matchTime, videoId from live_video where videoCategory = '2'";
$result = mysqli_query($conn, $sql);

//英超\意甲\西甲
$urlList = array (
	"http://sports.sina.com.cn/z/plvideo/",
	"http://sports.sina.com.cn/z/serieavideo/",
	"http://sports.sina.com.cn/z/laligavideo/"
);

foreach ($urlList as $url) {
	echo $url . "<br>";
	$url = file_get_contents($url);
	$url = substr($url, strpos($url, "URL=") + 4, strlen($url));
	$url = substr($url, 0, strpos($url, ">"));

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
	curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
	$HtmlContents = curl_exec($ch);
	curl_close($ch);

	$sql = "INSERT INTO live_video (matchTime, videoId, videoTitle, videoImgUrl, videoLength, videoUrl, homeTeam, visitTeam, videoCategory, updateDate) VALUES";

	$HtmlContents = str_get_html($HtmlContents);

	$teamList = $HtmlContents->find('div[class=listtop]');

	$i = 0;
	$j = 0;
	foreach ($teamList as $index => $team) {
		$homeTeam = null;
		$visitTeam = null;
		$teamName = iconv("gbk", "utf-8", $team->innertext);
		$teamName = trim($teamName);
		if (strpos($teamName, " ")) {

			$teamName = explode(" ", $teamName);
			//取得主队名称
			$homeTeam = $teamName[0];
			//取得客队名称
			$visitTeam = $teamName[2];

			$videoList = $HtmlContents->find('div[class=listmid]', $index);
			$videoList = $videoList->find('li');
			foreach ($videoList as $video) {

				$matchTime = $video->find('a', 0)->href;
				$matchTime = substr($matchTime, 38, 10);
				echo "matchTime：" . $matchTime;
				echo '<br>';
				$videoTitle = $video->find('a', 0)->plaintext;
				$videoTitle = iconv("gbk", "utf-8", $videoTitle);
				$videoTitle = str_replace("'", "’", $videoTitle);
				echo "title：" . $videoTitle;
				echo '<br>';
				echo "homeTeam：" . $homeTeam;
				echo '<br>';
				echo "visitTeam：" . $visitTeam;
				echo '<br>';
				$videoImgUrl = $video->find('a img ', 0)->src;
				echo "imgurl：" . $videoImgUrl;
				echo '<br>';
				$videoId = substr($videoImgUrl, strripos($videoImgUrl, "/") + 1, 10);
				echo "id：" . $videoId;
				echo '<br>';
				echo "videoLength：0";
				echo '<br>';
				$videoUrl = "http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=" . $videoId . "/s.swf";
				echo "videoUrl：" . $videoUrl;

				$insert = true;
				mysqli_data_seek($result, 0);
				while ($row_array = mysqli_fetch_array($result, MYSQL_ASSOC)) {
					if ($row_array["videoId"] == $videoId && $row_array["matchTime"] == $matchTime) {
						$insert = false;
						break;
					}
				}
				if ($insert) {
					$j = $j +1;
					$sql .= "('" . $matchTime . "', '" . $videoId . "', '" . $videoTitle . "', '" . $videoImgUrl . "', '', '" . $videoUrl . "', '" . $homeTeam . "', '" . $visitTeam . "', '2', now()),";
				}

				echo '<hr>';
			}
		} else {

		}
	}
	$sql = "SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

	if ($j > 0) {
		//连接数据库
		$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);
		//插入比赛比分
		mysqli_multi_query($conn, $sql);
	}
}
?>